package com.util;

import java.util.ArrayList;
import java.util.List;

public class TransferUtil {

//    public static void main(String[] args) {
//        String videoPath = "D:\\szw\\photo\\test\\55f9e02108b642.mp4";
//        String ffmpegPath = "src/main/resources/libs/ffmpeg.exe";
////        getVideoInfo(videoPath);
////        processImg(videoPath, "1", ffmpegPath);
//    }
    public static void main(String[] args) throws Exception {
        /*boolean flag = transform("E:\\百度网盘传输\\ffmpeg-20200220-56df829-win64-static\\bin\\ffmpeg.exe",
                "F:\\哔哩哔哩\\bbb\\425182036\\1\\425182036_1_0.mp4",
                "F:\\哔哩哔哩\\bbb\\425182036\\1\\425182036_1_0_480_320.mp4", "480x320");
        System.out.println(flag);*/
        //\937637551\1\937637551_1_0.mp4
        boolean flag1 = transform("E:\\百度网盘传输\\ffmpeg-20200220-56df829-win64-static\\bin\\ffmpeg.exe",
                "F:\\哔哩哔哩\\bbb\\255191664\\1\\255191664_1_0.mp4",
                "F:\\哔哩哔哩\\bbb\\255191664\\1\\255191664_1_0_480_320.mp4", "480x320");//_480_320
        System.out.println(flag1);
        boolean flag2 = transform("E:\\百度网盘传输\\ffmpeg-20200220-56df829-win64-static\\bin\\ffmpeg.exe",
                "F:\\哔哩哔哩\\bbb\\255191664\\1\\255191664_1_0.mp4",
                "F:\\哔哩哔哩\\bbb\\255191664\\1\\255191664_1_0_720_720.mp4", "720x720");//_720_720
        System.out.println(flag2);
        //\724599872\1\724599872_1_0.mp4
//        boolean flag = transform("D:\\ffmpeg\\ffmpeg2016\\bin\\ffmpeg.exe", "d:\\ys\\StoryBrooke.mp4", "d:\\ys\\480p.flv", "480x320");
    }

    /**
     * 视频转换
     * @param ffmpegPath ffmpeg路径
     * @param oldPath 原视频地址
     * @param newPath 新视频存放地址(包含视频格式)
     * @param resolution 分辨率
     * @return
     * @throws
     */
    public static Boolean transform(String ffmpegPath, String oldPath, String newPath, String resolution) throws  Exception {
        List<String> command = getFfmpegCommand(ffmpegPath, oldPath, newPath, resolution);
        if (null != command && command.size() > 0) {
            return process(command);
        }
        return false;
    }

    private static boolean process(List<String> command) throws Exception {
        try {
            if (null == command || command.size() == 0)
                return false;
            Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
            videoProcess.getInputStream().close();
            int exitcode = videoProcess.waitFor();
            if (exitcode == 1)
                return false;
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private static List<String> getFfmpegCommand(String ffmpegPath, String oldfilepath, String outputPath, String resolution) throws Exception {
        List<String> command = new ArrayList<String>();
        command.add(ffmpegPath); // 添加转换工具路径
        command.add("-i"); // 添加参数＂-i＂，该参数指定要转换的文件
        command.add(oldfilepath); // 添加要转换格式的视频文件的路径
        command.add("-qscale"); // 指定转换的质量
        command.add("4");
        
        /*command.add("-ab"); //设置音频码率
        command.add("64"); 
        command.add("-ac"); //设置声道数 
        command.add("2"); 
        command.add("-ar"); //设置声音的采样频率
        command.add("22050");*/
         
        command.add("-r"); // 设置帧速率
        command.add("24");
        command.add("-s"); // 设置分辨率
        command.add(resolution);
        command.add("-y"); // 添加参数＂-y＂，该参数指定将覆盖已存在的文件
        command.add(outputPath);
        return command;
    }
}

//class FFmpegException extends Exception {
//
//    private static final long serialVersionUID = 1L;
//
//    public FFmpegException() {
//        super();
//    }
//
//    public FFmpegException(String message) {
//        super(message);
//    }
//
//    public FFmpegException(Throwable cause) {
//        super(cause);
//    }
//
//    public FFmpegException(String message, Throwable cause) {
//        super(message, cause);
//    }
//}